package shop.up.all.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import shop.up.cart.client.CartFeignClient;
import shop.up.model.product.SkuInfo;
import shop.up.product.client.ProductFeignClient;

import javax.servlet.http.HttpServletRequest;

/**
 * 购物车接口 {@link CartController}
 *
 * @author Kevin
 * @date 2022/7/25
 */
@Controller
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    /**
     * 加入购物车
     * @param skuId
     * @param skuNum
     * @param request
     * @return
     */
    @RequestMapping("addCart.html")
    public String addCart(@RequestParam(name = "skuId") Long skuId,
                          @RequestParam(name = "skuNum") Integer skuNum,
                          HttpServletRequest request){
        // 远程调用，加入购物车
        cartFeignClient.addToCart(skuId, skuNum);
        // 远程调用，查询商品信息
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);

        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);

        return "cart/addCart";
    }

    /**
     * 查看购物车
     * @param request
     * @return
     */
    @RequestMapping("cart.html")
    public String index(HttpServletRequest request){
        return "cart/index";
    }
}
